import { createAction } from 'redux-actions';
import ColorectalCancerLogic from '../logics/ColorectalCancerLogic';
import GastricCancerLogic from '../logics/GastricCancerLogic';
import FiveHealthHabitLogic from '../logics/FiveHealthHabitLogic';
import CardiacInfarctionLogic from '../logics/CardiacInfarctionLogic';
import CerebralStrokeLogic from '../logics/CerebralStrokeLogic';
import CommonUtils from '../logics/CommonUtils';
import { BodyItemConstant as BiC } from '../constants/InputItemConstants';

const SAVE = 'SAVE';
const CLEAR = 'CLEAR';
const CALC_RISK_COLORECTAL_CANCER = 'CALC_RISK_COLORECTAL_CANCER';
const CALC_RISK_GASTRIC_CANCER = 'CALC_RISK_GASTRIC_CANCER';
const CALC_RISK_FIVE_HEALTH_HABIT = 'CALC_RISK_FIVE_HEALTH_HABIT';
const CALC_RISK_CEREBRAL_STROKE = 'CALC_RISK_CEREBRAL_STROKE';
const CALC_RISK_CARDIAC_INFARCTION = 'CALC_RISK_CARDIAC_INFARCTION';
const ROUTED_CATEGORY = 'ROUTED_CATEGORY';

const resetInitalRisk = (resultObj, isReset) => {
  if (isReset) {
    resultObj = {
      ...resultObj,
      initialRisk: resultObj.risk,
      initialAdvice: resultObj.advice,
      initialHealthy: resultObj.healthy,
    }
  }
  return resultObj;
};

const calcCommonValue = (data) => {
  return {
    bmi: CommonUtils.calcBMI(data),
    mets: CommonUtils.calcMets(data),
    ethanol: CommonUtils.calcEthanol(data),
  }
};

// 入力内容の補正
const correctValue = (inputNumData) => {
  // オブジェクトのコピーを返す
  return Object.assign({}, inputNumData, {
    // 飲酒量が未入力の場合、「飲まない」に補正
    [BiC.ALCOHOL_AMOUNT_BEER]: CommonUtils.ifNull(inputNumData[BiC.ALCOHOL_AMOUNT_BEER], BiC.ALCOHOL_AMOUNT_BEER.NO_DRINKING.value),
    [BiC.ALCOHOL_AMOUNT_WINE]: CommonUtils.ifNull(inputNumData[BiC.ALCOHOL_AMOUNT_WINE], BiC.ALCOHOL_AMOUNT_WINE.NO_DRINKING.value),
    [BiC.ALCOHOL_AMOUNT_SAKE]: CommonUtils.ifNull(inputNumData[BiC.ALCOHOL_AMOUNT_SAKE], BiC.ALCOHOL_AMOUNT_SAKE.NO_DRINKING.value),
    [BiC.ALCOHOL_AMOUNT_SHOCHU]: CommonUtils.ifNull(inputNumData[BiC.ALCOHOL_AMOUNT_SHOCHU], BiC.ALCOHOL_AMOUNT_SHOCHU.NO_DRINKING.value),
    [BiC.ALCOHOL_AMOUNT_WHISKEY]: CommonUtils.ifNull(inputNumData[BiC.ALCOHOL_AMOUNT_WHISKEY], BiC.ALCOHOL_AMOUNT_WHISKEY.NO_DRINKING.value),
  });
};

// 確定
export const save = createAction(SAVE, (inputData, pageIndex) => {
  return { inputData, pageIndex };
});

// ストア（入力、結果表示データ）のクリア
export const clear = createAction(CLEAR);

// 大腸がんのリスク算出
export const calcRiskOfColorectalCancer = createAction(CALC_RISK_COLORECTAL_CANCER, (data, isReset = false) => {
  let numData = correctValue(CommonUtils.copyAndString2Number(data));
  let commnData = calcCommonValue(numData);
  return resetInitalRisk({
    inputData: data,
    risk: CommonUtils.diagnosisRiskLevel(ColorectalCancerLogic.calcRisk(numData, commnData)),
    advice: ColorectalCancerLogic.takeAdvices(numData, commnData),
    healthy: ColorectalCancerLogic.getHealthy(numData),
  }, isReset);
});

// 胃がんのリスク算出
export const calcRiskOfGastricCancer = createAction(CALC_RISK_GASTRIC_CANCER, (data, isReset = false) => {
  let numData = correctValue(CommonUtils.copyAndString2Number(data));
  return resetInitalRisk({
    inputData: data,
    risk: CommonUtils.diagnosisRiskLevel(GastricCancerLogic.calcRisk(numData)),
    advice: GastricCancerLogic.takeAdvices(numData),
  }, isReset);
});

// 5つの健康習慣のリスク計算
// 引数の、fiveHealtHabitFlgsは、シミュレーション画面の場合に、設定される
export const calcRiskOfFiveHealthHabit = createAction(CALC_RISK_FIVE_HEALTH_HABIT, (data, isReset = false, fiveHealtHabitFlgs) => {
  let numData = correctValue(CommonUtils.copyAndString2Number(data));
  let commnData = calcCommonValue(numData);
  let commnMiddleData = FiveHealthHabitLogic.calcMiddleParameter(numData, commnData);
  return resetInitalRisk({
    inputData: data,
    /*
     * ５つの健康習慣を実施中かどうか管理するフラグ（健康習慣毎に、フラグが５つあり）
     * 初回は入力内容から計算したフラグを設定し、シミュレーション画面では、実施中の選択状態を引き継いて設定する。
     */
    fiveHealtHabitFlgs: fiveHealtHabitFlgs || FiveHealthHabitLogic.toFiveHealtHabitFlgs(commnMiddleData.extractKey),
    risk: CommonUtils.diagnosisRiskLevel(FiveHealthHabitLogic.calcRisk(numData, commnMiddleData, fiveHealtHabitFlgs)),
    advice: FiveHealthHabitLogic.takeAdvices(numData, commnMiddleData),
  }, isReset);
});

// 脳卒中のリスク計算
export const calcRiskOfCerebralStroke = createAction(CALC_RISK_CEREBRAL_STROKE, (data, isReset = false) => {
  let numData = correctValue(CommonUtils.copyAndString2Number(data));
  return resetInitalRisk({
    inputData: data,
    risk: CommonUtils.diagnosisRiskLevel(CerebralStrokeLogic.calcRisk(numData)),
    advice: CerebralStrokeLogic.takeAdvices(numData),
    healthy: CommonUtils.getHealthyCirculatoryDiseases(numData, true, false),
  }, isReset);
});

// 心筋梗塞のリスク計算
export const calcRiskOfCardiacInfarction = createAction(CALC_RISK_CARDIAC_INFARCTION, (data, isReset = false) => {
  // 心筋梗塞のリスク計算アクション
  let numData = correctValue(CommonUtils.copyAndString2Number(data));
  return resetInitalRisk({
    inputData: data,
    risk: CommonUtils.diagnosisRiskLevel(CardiacInfarctionLogic.calcRisk(numData)),
    advice: CardiacInfarctionLogic.takeAdvices(numData),
    healthy: CommonUtils.getHealthyCirculatoryDiseases(numData, false, true),
  }, isReset);
});

// すべてのリスク計算
export const calcRiskAll = (inputData, pageIndex) => (dispatch) => {
  // 入力内容を保存
  dispatch(save(inputData, pageIndex));
  // 大腸がんのリスクを算出
  dispatch(calcRiskOfColorectalCancer(inputData, true));
  // 胃がんリスクを算出
  dispatch(calcRiskOfGastricCancer(inputData, true));
  // ５つの健康習慣リスクを算出
  dispatch(calcRiskOfFiveHealthHabit(inputData, true));
  // 脳卒中リスクを算出
  dispatch(calcRiskOfCerebralStroke(inputData, true));
  // 心筋梗塞リスクを算出
  dispatch(calcRiskOfCardiacInfarction(inputData, true));
};

// 詳細に遷移
export const routedCategory = createAction(ROUTED_CATEGORY, (category) => {
  console.log(category)
  return { routedCategory: category };
});
